<?php

// 品牌分类管理

namespace app\console\controller;

use think\Url;
use think\Loader;
use think\Request;
use think\Controller;
use app\console\model\BrandType as BrandTypeModel;

class BrandType extends Controller
{

    /**
     * 品牌分类列表
     * @param Request $request
     * @param BrandTypeModel $brandType
     * @return mixed
     */
    public function index(Request $request, BrandTypeModel $brandType)
    {
        // 获取参数
        $inquire = $request->instance()->param();

        // 搜索条件
        $condition['id'] = ['gt', 0];
        if (!empty($inquire['title'])) $condition['title'] = ['like', '%' . $inquire['title'] . '%'];

        // 读取参数
        $data = $brandType->where($condition)->order(['sort' => 'asc', 'id' => 'asc'])->paginate(20, false, ['query' => $inquire]);


        return $this->fetch('index', [
            'mate_title' => '品牌分类列表',
            'data'       => $data
        ]);
    }


    /**
     * 新增品牌分类
     * @param Request $request
     * @param BrandTypeModel $brandType
     * @return mixed
     */
    public function create(Request $request, BrandTypeModel $brandType)
    {

        if ($request->instance()->isPost()) {

            $data = $request->instance()->param();

            // 验证
            $validate = Loader::validate('BrandType');
            if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 提交数据
            $dispose = $brandType->allowField(true)->save($data);

            if ($dispose) {
                win('操作成功', Url::build('BrandType/index'));
            }

            fail('操作失败');

        }


        return $this->fetch('create', [
            "mate_title" => '新增品牌分类',

        ]);

    }


    /**
     * 删除品牌分类
     * @param $id
     * @param BrandTypeModel $brandType
     */
    public function destroy($id, BrandTypeModel $brandType)
    {

        if ($brandType::destroy($id)) {
            win('操作成功', Url::build('BrandType/index'));
        }

        fail('操作失败');

    }


    /**
     * 编辑品牌分类
     * @param $id
     * @param Request $request
     * @param BrandTypeModel $brandType
     * @return mixed
     */
    public function update($id, Request $request, BrandTypeModel $brandType)
    {

        if ($request->instance()->isPost()) {

            $data = $request->instance()->param();

            // 验证
            $validate = Loader::validate('BrandType');
            if (!$validate->scene('create')->check($data)) fail($validate->getError());

            // 提交数据
            $dispose = $brandType->allowField(true)->save($data, ['id' => $id]);

            if ($dispose) {
                win('操作成功', Url::build('BrandType/index'));
            }

            fail('操作失败');

        }

        $item = $brandType::get($id);


        return $this->fetch('create', [
            "mate_title" => '编辑品牌分类',
            'item'       => $item

        ]);
    }


    /**
     * 更改状态
     * @param Request $request
     * @param BrandTypeModel $brandType
     * @return bool|int
     */
    public function auditing(Request $request, BrandTypeModel $brandType)
    {
        $id = $request->instance()->param('id');

        $status = $brandType->where('id', $id)->value('status') == '1' ? 2 : 1;

        if ($brandType->where('id', $id)->update(['status' => $status])) {

            return $status;
        }

        return false;
    }


    /**
     * 更改排序
     * @param Request $request
     * @param BrandTypeModel $brandType
     */
    public function sorting(Request $request, BrandTypeModel $brandType)
    {

        $id = $request->instance()->param('orderId/a');
        $sort = $request->instance()->param('sort/a');

        foreach ($id as $key => $value) {

            $brandType->where('id', $value)->update(['sort' => $sort[$key]]);

        }

        win('操作成功', Url::build('BrandType/index'));
    }
}
